<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1600" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" align-center>
        <el-header>
            <div class="left-panel">
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.purchase_code" placeholder="订单编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.purchase_code" placeholder="采购编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable @change="applyChange(form.apply_id)">
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select>
                    <el-select v-model="search.plan_type" placeholder="订单类型" clearable>
                        <el-option label="白坯" :value="1"></el-option>
                        <el-option label="半成品" :value="2"></el-option>
                        <el-option label="成品" :value="3"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-input v-model="search.purchase_code" placeholder="关联" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.status" placeholder="请选择状态" clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="-1"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border :params="query" height="700">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <el-table-column label="入库类型" prop="purchase_dye_code" width="150">
                    <template #default="{row}">
                        <el-tag :type="row.input_type == 1 ? 'info' : row.input_type == 2 ? 'primary' : row.input_type == 3 ? 'success' : 'warning'">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag>
                        <el-link :underline="false" type="primary" @click="table_show_dye(row)">{{ row.dye.purchase_dye_code }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="入库单号" prop="apply.apply_name" width="200">
                    <template #default="{row}">
                        <div style="display: flex;align-items: center;margin: 10px 0;">
                            <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_ware_code }}</el-link>
                            <el-icon class="icon" color="#1e9fff" :size="20" @click="purchase_bell(row)"><el-icon-BellFilled /></el-icon>
                        </div>
                        <div>{{row.ware_date}}({{row.ware_name}})</div>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="date">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.apply.apply_name}}</div>
                        <div>{{row.apply.linkman}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="染色方">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory.apply_name:''}}</div>
                        <div>{{row.dye_factory?row.dyeFactory.linkman:''}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="加工方" align="center">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.process_factory?row.processFactory.apply_name:''}}</div>
                        <div>{{row.process_factory?row.processFactory.linkman:''}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="品名" prop="goods_name" align="center"></el-table-column>
                <el-table-column label="数量(匹)" prop="total_num" align="center"></el-table-column>
                <el-table-column label="单价(元/米)" prop="price" align="center"></el-table-column>
                <el-table-column label="审核" prop="status" align="center">
                    <template #default="{row}">
                        <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'dyehouse.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="文件" prop="upload" align="center">
                    <template #default="{row}">
                        <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'dyehouse.upload'">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="" align="center"></el-table-column>
                <el-table-column label="业务员" prop="createstaff.name" align="center"></el-table-column>
            </scTable>
        </el-main>
    </el-dialog>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <dye-dialog v-if="dialog.dye" ref="dyeDialog" @success="handleSuccess" @closed="dialog.dye = false"></dye-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
</template>

<script>

import saveDialog from './save'
import dyeDialog from '../order/save'
import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
export default {
    props: {
        query: { type: Object, default: () => ({}) },
    },
	emits: ['success', 'closed'],
    components: {
        saveDialog, dyeDialog, CheckStatus, uploadDialog
    },
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '入库记录',
                copy: '新增染色入库',
                edit: '编辑染色入库',
                show: '查看染色入库',
            },
            visible: false,
            isSaveing: false,
            dialog: {
                save: false,
                dye: false,
                check: false,
                upload: false,
            },
            apiObj: this.$API.business.dye.house.list,
            selection: [],
            search: {
                purchase_code: null,
                date: null,
                status: null,
                goods_name: null
            },
            applyList: [],
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'dye_house') {
                this.$refs.table.refresh()
            }
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get();
			this.applyList = res.data
		},
        // 入库单
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        // 染色单
        table_show_dye(row){
            let query = { id: row.dye_id, dye_code: row.dye.purchase_dye_code }
            this.dialog.dye = true
            this.$nextTick(() => {
                this.$refs.dyeDialog.open('show').setData(query)
            })
        },
        // 采购审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('dye_house').setData(row)
            })
        },
        // 查看文件
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
    }
}
</script>

<style lang="scss" scoped></style>
